﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IceDog.Practice.WinForm.UserControls
{
    [Designer(typeof(LabelTextBoxRules))]
    public partial class LabelTextBox : UserControl
    {
        public enum LTBPosition
        {
            /// <summary>
            /// 右边
            /// </summary>
            Right,
            /// <summary>
            /// 下边
            /// </summary>
            Below
        }

        private LTBPosition _positon;
        private int _textBoxMargin;
        private string _propName;
        private string _propValue;

        public LabelTextBox()
        {
            InitializeComponent();
            _propName = "label";
            _textBoxMargin = 4;
            _positon = LTBPosition.Right;
        }

        /// <summary>
        /// textbox相对于label的位置
        /// </summary>
        /// <value>textbox相对于label的位置</value>
        public LTBPosition Position
        {
            get
            {
                return _positon;
            }
            set
            {
                _positon = value;
                MoveControls();
                if (PositionChanged!=null)
                {
                    PositionChanged(this, new EventArgs());
                }
            }
        }
        /// <summary>
        /// label和textbox的间距
        /// </summary>
        public int TextBoxMargin
        {
            get
            {
                return _textBoxMargin;
            }
            set
            {
                _textBoxMargin = value;
                MoveControls();
            }
        }
        /// <summary>
        /// label的值
        /// </summary>
        public String PropName
        {
            get
            {
                return _propName;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    _propName = "label";
                }
                else
                {
                    _propName = value;
                }
                lblName.Text = _propName;
                MoveControls();
            }
        }
        /// <summary>
        /// textbox的值
        /// </summary>
        public string PropValue
        {
            get
            {
                return _propValue;
            }
            set
            {
                _propValue = value;
                tbValue.Text = _propValue;
                MoveControls();
            }
        }
        public event EventHandler PositionChanged;
        private void LabelTextBox_Load(object sender, EventArgs e)
        {
            MoveControls();
        }

        private void LabelTextBox_SizeChanged(object sender, EventArgs e)
        {
            MoveControls();
        }

        private void MoveControls()
        {
            var l = lblName;
            var t = tbValue;
            switch (_positon)
            {
                case LTBPosition.Below:
                    t.Top = l.Bottom + 4;
                    t.Left = l.Left;
                    t.Width = Width - 8;
                    Height = 30 + t.Height;
                    break;
                case LTBPosition.Right:
                    t.Top = l.Top - 5;
                    t.Left = l.Right + _textBoxMargin;
                    t.Width = Width - t.Left - 4;
                    Height = 30;
                    break;
            }
        }

        private void TbValue_KeyDown(object sender, KeyEventArgs e)
        {
            OnKeyDown(e);
        }

        private void TbValue_KeyPress(object sender, KeyPressEventArgs e)
        {
            OnKeyPress(e);
        }

        private void TbValue_KeyUp(object sender, KeyEventArgs e)
        {
            OnKeyUp(e);
        }
    }
}
